package com.nari.ysphd.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.jeremyfeinstein.gson.Meeting;
import com.jeremyfeinstein.gson.OnResponse;
import com.jeremyfeinstein.okhttputil.callback.StringCallback;
import com.jeremyfeinstein.utils.AppConstants;
import com.jeremyfeinstein.utils.ToastUtil;
import com.jeremyfeinstein.utils.YCHShttpAPI;
import com.nari.ysphd.MyApp;
import com.nari.ysphd.R;
import com.nari.ysphd.adapter.ForthcomingMeetingsAdapter;
import com.nari.ysphd.base.AbsAdapter;
import com.nari.ysphd.base.BaseMeetingFragment;
import com.nari.ysphd.listener.OnForthMeetingListener;
import com.nari.ysphd.ui.IPConfigActivity;
import com.nari.ysphd.ui.MeetingManageUI;
import com.nari.ysphd.ui.TimeOutActivity;
import com.nari.ysphd.util.ActivityManager;

import java.util.List;

import nari.mip.core.Platform;
import okhttp3.Call;

/**
 * Create by sunxunchao on 2019/3/29
 * 即将开始的互动
 */
public class ForthcomingMeetingsFragment extends BaseMeetingFragment {

    private static final String TAG = "ForthMeetingsFragment";
    private ForthcomingMeetingsAdapter adapter;
    private Dialog dialog;
    private ProgressDialog progressDialog;

    private static final String MEETING_OF_WORD = "meetingOfWord";

    public static Fragment newInstance(String meetingOfWord){
        Bundle bundle=new Bundle();
        bundle.putString(MEETING_OF_WORD, meetingOfWord);
        ForthcomingMeetingsFragment f = new ForthcomingMeetingsFragment();
        f.setArguments(bundle);
        return f;
    }

    @Override
    protected void initViews(View rootView) {
        super.initViews(rootView);
        adapter.setOnForthMeetingListener(listener);

    }

    @Override
    protected AbsAdapter getAdapter() {
        adapter = new ForthcomingMeetingsAdapter(getActivity(), R.layout.adapter_forthcoming_meetings);
        return adapter;
    }

    private OnForthMeetingListener listener = new OnForthMeetingListener() {
        @Override
        public void onEditClicked(View view, Meeting meeting) {
            if (!"1".equals(meeting.getStatus())) {
                Toast.makeText(getActivity(), "只能编辑未开始的互动", Toast.LENGTH_LONG).show();
                return;
            }
            Intent intent = new Intent(getActivity(), MeetingManageUI.class);
            intent.putExtra(MeetingManageUI.BUNDLE_MEETING_ID, meeting.getMeetingId());
            startActivity(intent);
        }

        @Override
        public void onDeleteClicked(View view, final Meeting meeting) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage("确定删除" + meeting.getMeetingName() + "这个互动吗？");
            builder.setTitle("提示");
            builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    progressDialog = ProgressDialog.show(getActivity(), null, "正在删除...");
                    deleteMeeting(meeting);
                }
            });
            builder.setNegativeButton("否", null);
            dialog = builder.create();
            dialog.show();
        }
    };


    @Override
    protected String getMeetingListType() {
        return "0";
    }

    @Override
    protected String getMeetingOfWords() {
        return getArguments().getString(MEETING_OF_WORD);
    }

    private void deleteMeeting(final Meeting meeting) {
        YCHShttpAPI.deleteMeeting(getActivity(), meeting.getMeetingId(), new StringCallback() {

            @Override
            public void onResponse(String response) {
                progressDialogDismiss();
                OnResponse onResponse = YCHShttpAPI.getOnResponse(response);
                if ("0".equals(onResponse.getResultCode())) {
                    List<Meeting> meetings = adapter.getDatas();
                    if (meetings != null && meetings.contains(meeting)) {
                        Toast.makeText(getActivity(), "删除成功！", Toast.LENGTH_SHORT).show();
                        adapter.deleteItem(meeting);
                    }
                } else if ("-2".equals(onResponse.getResultCode())){
                    Intent intent = new Intent(getActivity(), TimeOutActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } else {
//                    if (AppConstants.TAG_LOGINOUT.equals(onResponse.getErrorMessage())){
//                        ActivityManager.getInstance().finishActivityExcept(IPConfigActivity.class);
//                        Platform.getCurrent().stop(MyApp.getApp());
//                        ToastUtil.showLong(MyApp.getApp(),onResponse.getErrorMessage());
//                        return;
//                    }
                    if (TextUtils.isEmpty(onResponse.getErrorMessage())) {
                        Toast.makeText(getActivity(), "删除出错请重试", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), String.valueOf(onResponse.getErrorMessage()), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onError(Call call, int statusCode, Exception e) {
                progressDialogDismiss();
                Toast.makeText(getActivity(), "删除出错请重试", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "onError: ", e);
            }
        });
    }

    private void progressDialogDismiss() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onDestroy() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        super.onDestroy();
    }
}
